package self.hoy.demo.grainmall;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * session整合redis相关配置， 需配合 @EnableRedisSession 使用
 * @author hyh
 * @date 2020/8/14
 */
@Configuration
@ConditionalOnClass(LettuceConnectionFactory.class)
@EnableRedisHttpSession //redis整合session共享
public class GrainSessionConfiguration {
    @Value("${spring.redis.port:16379}")
    private Integer port;

    @Value("${spring.redis.host:127.0.0.1}")
    private String host;

    @Value("${spring.redis.database:1}")
    private Integer database;

    @Value("${spring.redis.password:}")
    private String password;

    @Value("${site.domain}")
    private String domain;

    /**
     *
     * 给redis使用，目前用于session-redis整合
     * @return
     */
    @Bean
    public LettuceConnectionFactory connectionFactory() {
//        return new LettuceConnectionFactory();
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(database);
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        if (!StringUtils.isEmpty(password)) {
            redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        }

        LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration.builder();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration,
                lettuceClientConfigurationBuilder.build());
        return factory;
    }

    /**
     * 自定义cookie的name和domain，用于跨域
     */
    @Bean
    public CookieSerializer cookieSerializer(){
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setDomainName(domain);
        serializer.setCookieName("GRAIN-SESSION");
        return serializer;
    }

    /**
     * 这个默认是给session序列化用的，还不能直接给CacheManager用
     * @return
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
